package com.example.sp03.service;

import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.UserService;
import cn.tedu.web.util.JsonUtil;
import com.example.sp03.config.MyUserProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/14 15:25
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private MyUserProperties myUserProperties;



    /**
     * 若不存在预置列表中，则返回 id,新用户:id,密码:id
     * @param id
     * @return
     */
    @Override
    public User getUser(Integer id) {
        log.info("获取用户，userId={}",id);
        final List<User> list = JsonUtil.from(myUserProperties.getUsers(), new TypeReference<List<User>>() {
        });
        for (User user : list) {
            if (Objects.equals(id, user.getId())) {
                return user;
            }
        }
        return new User(id,String.format("新用户:%s",id),String.format("密码:%s",id));
    }


    @Override
    public void addScore(Integer id, Integer score) {
        log.info("增加用户积分,userId={},score={}",id,score);

    }

    @Override
    public List<User> getUserList() {
        return JsonUtil.from(myUserProperties.getUsers(), new TypeReference<List<User>>() {
        });
    }


}
